package com.scorpionstudio.dantripro;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.scorpionstudio.adapter.RssAdapter;
import com.scorpionstudio.bean.News;
import com.scorpionstudio.control.ParseXMLRss;
import com.scorpionstudio.utils.Constant;
import com.scorpionstudio.utils.MyDialog;

public class ListMenuNews extends Activity implements OnItemClickListener {
	private String TAG = ListMenuNews.class.getSimpleName();
	private ListView lvMenu;
	private RssAdapter mNewsAdapter;
	MyDialog mDialog;
	ArrayList<News> result;
	private String[] listLink;
	private int keyIndex = 0;
	String nameMenu = "";
	private TextView tvNameMenu;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_list_menu);
		initVariable();
		Bundle bdGetIndex = getIntent().getExtras();
		if (bdGetIndex != null) {
			keyIndex = bdGetIndex.getInt(Constant.KEY_INDEX_MENU);
			nameMenu = bdGetIndex.getString(Constant.KEY_NAME_MENU);
			switch (keyIndex) {
			case 0:
				new getAllDetail().execute(listLink[0]);
				tvNameMenu.setText(nameMenu);
				break;
			case 1:
				new getAllDetail().execute(listLink[1]);
				tvNameMenu.setText(nameMenu);
				break;
			case 2:
				new getAllDetail().execute(listLink[2]);
				tvNameMenu.setText(nameMenu);
				break;
			case 3:
				new getAllDetail().execute(listLink[3]);
				tvNameMenu.setText(nameMenu);
				break;
			case 4:
				new getAllDetail().execute(listLink[4]);
				tvNameMenu.setText(nameMenu);
				break;
			case 5:
				new getAllDetail().execute(listLink[5]);
				tvNameMenu.setText(nameMenu);
				break;
			case 6:
				new getAllDetail().execute(listLink[6]);
				tvNameMenu.setText(nameMenu);
				break;
			case 7:
				new getAllDetail().execute(listLink[7]);
				tvNameMenu.setText(nameMenu);
				break;
			case 8:
				new getAllDetail().execute(listLink[8]);
				tvNameMenu.setText(nameMenu);
				break;
			case 9:
				new getAllDetail().execute(listLink[9]);
				tvNameMenu.setText(nameMenu);
				break;
			case 10:
				new getAllDetail().execute(listLink[10]);
				tvNameMenu.setText(nameMenu);
				break;
			case 11:
				new getAllDetail().execute(listLink[11]);
				tvNameMenu.setText(nameMenu);
				break;
			case 12:
				new getAllDetail().execute(listLink[12]);
				tvNameMenu.setText(nameMenu);
				break;
			case 13:
				new getAllDetail().execute(listLink[13]);
				tvNameMenu.setText(nameMenu);
				break;
			case 14:
				new getAllDetail().execute(listLink[14]);
				tvNameMenu.setText(nameMenu);
				break;
			case 15:
				new getAllDetail().execute(listLink[15]);
				tvNameMenu.setText(nameMenu);
				break;
			case 16:
				new getAllDetail().execute(listLink[16]);
				tvNameMenu.setText(nameMenu);
				break;
			case 17:
				new getAllDetail().execute(listLink[17]);
				tvNameMenu.setText(nameMenu);
				break;
			case 18:
				new getAllDetail().execute(listLink[18]);
				tvNameMenu.setText(nameMenu);
				break;
			case 19:
				new getAllDetail().execute(listLink[19]);
				tvNameMenu.setText(nameMenu);
				break;

			default:
				break;
			}
		}
	}

	private void initVariable() {
		lvMenu = (ListView) findViewById(R.id.lv_menu);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View header = inflater.inflate(R.layout.header_view, null);
        lvMenu.addHeaderView(header, null, false);
        lvMenu.addFooterView(header, null, false);
		lvMenu.setOnItemClickListener(this);
		listLink = getResources().getStringArray(R.array.link_tin247);
		tvNameMenu = (TextView) findViewById(R.id.tv_name_menu);
		result = new ArrayList<News>();
		mDialog = new MyDialog(ListMenuNews.this,R.drawable.spinner);
		mDialog.setCancelable(true);
		mDialog.setCanceledOnTouchOutside(false);
		mDialog.setOnCancelListener(new OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				dialog.dismiss();
				Intent iBackmain = new Intent(ListMenuNews.this,Main.class);
				iBackmain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(iBackmain);
				finish();
			}
		});
		
	}


	private class getAllDetail extends AsyncTask<String, Void, ArrayList<News>> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mDialog.show();
		}

		@Override
		protected ArrayList<News> doInBackground(String... params) {
			try {
				URL url = new URL(params[0]);
				URLConnection ucon = url.openConnection();
				ucon.connect();
				InputStream mIs = ucon.getInputStream();
				result = ParseXMLRss.getDataFromXML(mIs);
			} catch (Exception e) {
				Log.e(TAG, e.toString());
			}
			return result;
		}

		@Override
		protected void onPostExecute(ArrayList<News> result) {
			mNewsAdapter = new RssAdapter(ListMenuNews.this,
					android.R.layout.simple_list_item_1, result);
			lvMenu.setAdapter(mNewsAdapter);
			mDialog.dismiss();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent iGoInfo = new Intent(ListMenuNews.this,ShowInfoLink.class);
		iGoInfo.putExtra(Constant.KEY_SEND_LINK, result.get(position).getmUrl());
//		iGoInfo.putExtra(Constant.KEY_INDEX_SEND_BACK, keyIndex);
		startActivityForResult(iGoInfo,100);
//		finish();
	}
	@Override
	public void onBackPressed() {
		Intent iBackmain = new Intent(ListMenuNews.this,Main.class);
		iBackmain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(iBackmain);
		finish();
	}
}
